package com.cn.jinl.service;

import com.cn.jinl.api.IMaterialService;
import com.cn.jinl.dao.MaterialMapper;
import com.cn.jinl.domain.Material;
import com.cn.jinl.domain.MaterialExample;
import com.cn.jinl.utils.CollectionsUtil;
import com.cn.jinl.utils.CommonUtil;
import com.cn.jinl.utils.Constants;
import com.cn.jinl.utils.QueryForm;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * Created On : 2023/12/5.
 * <p>
 * Author : Huiyan
 * <p>
 * Description: MaterialServiceImpl
 */
@Service
public class MaterialServiceImpl implements IMaterialService {

    @Autowired
    private MaterialMapper materialMapper;


    @Override
    public List<Material> getSingleMaterialSelect() throws Exception {
        MaterialExample example = new MaterialExample();
        example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS).andMaterialStatusEqualTo("0");
        return materialMapper.selectByExample(example);
    }

    @Override
    public List<Material> getAllMaterialList() throws Exception {
        List<Material> list = materialMapper.selectAllMaterialList();
        return list;
    }

    @Override
    public List<Material> getMaterialWithItemName() throws Exception {
        return  materialMapper.selectMaterialWithItemName();
    }

    @Override
    public List<Material> getMaterialSelect(QueryForm queryForm) throws Exception {
        List<Material> list = materialMapper.selectMaterialList(queryForm);
        return list;
    }

    @Override
    public String updateMaterial(Material material) throws Exception {
        String id = material.getId();

        if (StringUtils.isEmpty(id)){
            return "id is null";
        }

        String result = this.materialLegal(material);

        //判断修改的物料数据是否满足字段的必填条件
        if (!"".equals(result)){
            return result;
        }

        if (this.countMaterialCode(material.getMaterialCode(),id) > 0){
            return "物料号已存在";
        }

        MaterialExample example = new MaterialExample();
        example.createCriteria().andIdEqualTo(id).andStatusEqualTo(Constants.NORMAL_STATUS);

        int count = materialMapper.updateByExampleSelective(material,example);

        return CommonUtil.outStr(count);
    }

    @Override
    public String deleteMaterial(QueryForm queryForm) throws Exception {
        List<String> ids = queryForm.getIds();
        if (CollectionsUtil.isEmpty(ids)){
            return "deleteMaterial ids is null";
        }
        //TODO 物料表可能关联其他设备表，做判断是否可以删除

        Material material = new Material();
        material.setStatus(Constants.DEL_STATUS);
        MaterialExample example = new MaterialExample();
        example.createCriteria().andIdIn(ids);
        int count = materialMapper.updateByExampleSelective(material,example);
        return CommonUtil.outStr(count);
    }

    @Override
    public String addMaterial(List<Material> materials) throws Exception {
        if (CollectionsUtil.isEmpty(materials)){
            return "请添加物料数据";
        }

        List<String> materialCodes = Lists.newArrayList();
        for (Material material:materials){

            String result = this.materialLegal(material);

            //判断修改的物料数据是否满足字段的必填条件
            if (!"".equals(result)){
                return result;
            }


            String materialId = CommonUtil.generateRandomNum("material_");
            material.setId(materialId);
            material.setStatus(Constants.NORMAL_STATUS);

            materialCodes.add(material.getMaterialCode());
        }

        MaterialExample example = new MaterialExample();
        example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS).andMaterialCodeIn(materialCodes);
        int existsCount = materialMapper.countByExample(example);
        if (existsCount > 0){
            return "物料代码已存在！";
        }

        int count = materialMapper.batchInsertMaterials(materials);

        return CommonUtil.outStr(count);
    }

    @Override
    public Map<String, List<Material>> getMaterialByGroupBigTypeId() throws Exception {
        List<Material> materials = materialMapper.selectMaterialWithItemName();
        if(CollectionUtils.isEmpty(materials)){
            return Maps.newHashMap();
        }
        Map<String, List<Material>> resultMap = Maps.newHashMap();
        for(Material material:materials){
            String itemName = material.getItemName();
            if(StringUtils.isEmpty(itemName)){
                continue;
            }
            if(resultMap.containsKey(itemName)){
                resultMap.get(itemName).add(material);
            }else {
                List<Material> adds = Lists.newArrayList();
                adds.add(material);
                resultMap.put(itemName,adds);
            }
        }
        return resultMap;
    }

    private int countMaterialCode(String code,String id) {
        MaterialExample example = new MaterialExample();
        MaterialExample.Criteria criteria = example.createCriteria();

        criteria.andStatusEqualTo(Constants.NORMAL_STATUS).andMaterialCodeEqualTo(code);
        if (StringUtils.isNotEmpty(id)){
            criteria.andIdNotEqualTo(id);
        }

        int count = materialMapper.countByExample(example);
        return count;
    }

    private String materialLegal(Material material){

        String result = "";

        if (StringUtils.isBlank(material.getCategoryId())){
            result = "请选择品类";
            return result;
        }

        if (StringUtils.isBlank(material.getMaterialCode())){
            result = "请输入物料代码";
            return result;
        }

//        if (StringUtils.isBlank(material.getFormatMaterial())){
//            result = "请输入物料规格";
//            return result;
//        }

        if (StringUtils.isBlank(material.getMeasureUnitId())){
            result = "请选择计量单位";
            return result;
        }

        if (StringUtils.isBlank(material.getMaterialStatus())){
            result = "请选择是否启用";
            return result;
        }
        return result;
    }


}
